package com.galaxy.device.controller;

import com.galaxy.common.core.domain.R;
import com.galaxy.device.domain.DeviceEs;
import com.galaxy.device.service.IDeviceEsService;
import lombok.RequiredArgsConstructor;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 设备ES控制器
 */
@RestController
@RequestMapping("/device/es")
@RequiredArgsConstructor
public class DeviceEsController {

    private final IDeviceEsService deviceEsService;

    /**
     * 保存设备信息到ES
     */
    @PostMapping("/save")
    public R<Boolean> saveDevice(@RequestBody DeviceEs deviceEs) {
        return R.ok(deviceEsService.saveDevice(deviceEs));
    }

    /**
     * 批量保存设备信息到ES
     */
    @PostMapping("/saveBatch")
    public R<Boolean> saveBatchDevice(@RequestBody List<DeviceEs> deviceEsList) {
        return R.ok(deviceEsService.saveBatchDevice(deviceEsList));
    }

    /**
     * 更新设备信息
     */
    @PutMapping("/update")
    public R<Boolean> updateDevice(@RequestBody DeviceEs deviceEs) {
        return R.ok(deviceEsService.updateDevice(deviceEs));
    }

    /**
     * 删除设备信息
     */
    @DeleteMapping("/{deviceId}")
    public R<Boolean> deleteDevice(@PathVariable String deviceId) {
        return R.ok(deviceEsService.deleteDevice(deviceId));
    }

    /**
     * 查询指定范围内的设备
     */
    @GetMapping("/nearby")
    public R<List<DeviceEs>> listNearbyDevices(
            @RequestParam Double longitude,
            @RequestParam Double latitude,
            @RequestParam(defaultValue = "5") Double distance) throws IOException {
        return R.ok(deviceEsService.listNearbyDevices(longitude, latitude, distance));
    }

    /**
     * 分页查询指定范围内的设备
     */
    @GetMapping("/nearby/page")
    public R<EsPageInfo<DeviceEs>> pageNearbyDevices(
            @RequestParam Double longitude,
            @RequestParam Double latitude,
            @RequestParam(defaultValue = "5") Double distance,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) throws IOException {
        return R.ok(deviceEsService.pageNearbyDevices(longitude, latitude, distance, pageNum, pageSize));
    }

    /**
     * 查询圆形范围内的设备
     * @param longitude 中心点经度
     * @param latitude 中心点纬度
     * @param radius 半径（单位：公里）
     * @return 设备列表
     */
    @GetMapping("/circle")
    public R<List<DeviceEs>> listDevicesInCircle(
            @RequestParam Double longitude,
            @RequestParam Double latitude,
            @RequestParam(defaultValue = "5") Double radius) throws IOException {
        return R.ok(deviceEsService.listDevicesInCircle(longitude, latitude, radius));
    }

    /**
     * 分页查询圆形范围内的设备
     * @param longitude 中心点经度
     * @param latitude 中心点纬度
     * @param radius 半径（单位：公里）
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页设备列表
     */
    @GetMapping("/circle/page")
    public R<EsPageInfo<DeviceEs>> pageDevicesInCircle(
            @RequestParam Double longitude,
            @RequestParam Double latitude,
            @RequestParam(defaultValue = "5") Double radius,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) throws IOException {
        return R.ok(deviceEsService.pageDevicesInCircle(longitude, latitude, radius, pageNum, pageSize));
    }
}
